package client.global.commlib.toast

import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.TextView
import android.widget.Toast
import client.global.commlib.ContextProvider
import client.global.commlib.R


class ToastFactory {

    companion object {
        private val instant by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { ToastFactory() }
        fun get() = instant
        var isSnackBack = false
        var toast: Toast? = null
    }

    fun createToast(message: String?) {
        if (message.isNullOrEmpty())
            return
        createNormalToast(message)
    }

    private fun createNormalToast(message: String?) {
        if (message.isNullOrEmpty())
            return



        ContextProvider.get().activity?.let {
//            isSnackBack = true
//            SnackBarUtil.snackBar(it, message)
            if (toast == null) {
                toast = Toast.makeText(it, message, Toast.LENGTH_SHORT).apply {
                    val view =
                        LayoutInflater.from(it).inflate(R.layout.view_toast_draw, null, false)
                    view?.findViewById<TextView>(R.id.message)?.text = message
                    setGravity(Gravity.CENTER, 0, 0)
                    setView(view)
                }
            } else {
                toast?.view?.findViewById<TextView>(R.id.message)?.text = message
            }
            toast?.show()
//            JToastDialog(it).apply {
//                msg = message
//            }.show()
        }
    }


}